package com.samphanie.auiu.auth.validation.code.captcha.Image;

import cn.hutool.core.util.IdUtil;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.samphanie.auiu.auth.security.properties.ValidateProperties;
import com.samphanie.auiu.auth.validation.code.captcha.Captcha;
import com.samphanie.auiu.auth.validation.code.captcha.service.ValidateCodeGenerator;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.ServletWebRequest;

import java.util.Date;

/**
 * @author ZSY
 * @email 1451691457@qq.com
 */
@RequiredArgsConstructor
@Component("imageValidateCodeGenerator")
public class ImageCodeGenerator implements ValidateCodeGenerator {

    private final DefaultKaptcha kaptcha;
    private final ValidateProperties validateProperties;

    @Override
    public Captcha generate(ServletWebRequest request) {

        String code = kaptcha.createText();
        return Captcha.builder()
                .uuid(IdUtil.simpleUUID())
                .code(code)
                .expireTime(Date.from(new Date().toInstant().plusSeconds(validateProperties.getKaptcha().getExpireIn())))
                .build();
    }
}
